/**
 *@description:上海银行 信用卡、借记卡 公共业务处理
 *@author: LiTao
 *@date:2018/8/24
  */

/**
 * 上海银行请求链接
 * @type {{}}
 */
var sh_urls={
    //官网首页
    INDEX_URL:"https://ebanks.bankofshanghai.com/pweb/welcome.do",
    //卡信息
    CRADINFO_URL:"https://ebanks.bankofshanghai.com/pweb/ActViewPre.do?_navigator=pquery.pactqry.ActView"

}

var sh_comm_handle={
    /**
     * 抓取首页
     */
    crawlIndex:function() {
    var indexHtml=ajax_get(sh_urls.INDEX_URL);
    storeDataToFile(context,indexHtml, "index.html");
    //添加
    context.put(keys.INDEX_PAGE,indexHtml);
},

/**
 * 解析首页，获取用户名
 */
 parseIndex:function(){
    //从上下文获取首页
    try {
        var indexHtml = context.get(keys.INDEX_PAGE);
        var accountDoc = Jsoup.parse(indexHtml);
        var name_on_card = accountDoc.select("p.siz16").get(0).text().trim().replaceAll(",", "").replaceAll("，", "");
        name_on_card = name_on_card.substring(0, name_on_card.length - 2);
        logger.info("name_on_card=="+name_on_card);
        //添加
        context.put(keys.USER_NAME,name_on_card);
    } catch (e) {
        logger.info("用户名解析失败",e.getMessage());
        statCaughtExceptionByJS(context,e);
    }
},

/**
 * 抓取卡信息
 */
 crawlCardInfo :function(){
    var cardListHtml = ajax_get(sh_urls.CRADINFO_URL);
    storeDataToFile(context,cardListHtml, "cardList.html");
    //添加卡列表
    context.put(keys.SESSION_INFO,cardListHtml);
},

/**
 * 解析卡列表，抓取卡的基础信息
 */
 parseCardInfo:function(){
    //从上下文获取卡列表
    var cardListHtml=context.get(keys.SESSION_INFO);
    var doc = Jsoup.parse(cardListHtml);

    //获取所有卡元素
    var elements = doc.select("body > div > form > div.Box1 > div.content_all > table > tbody > tr");

    if (elements.isEmpty()) {
        throw new 	BankException(BankConstants.BALO_20304_30,"解析账户列表页面失败，停止抓取");
    }

    //存放信用卡信息
    var creditCardInfoList=new ArrayList();

    //存放借记卡信息
    var debitCardInfoList=new ArrayList();

    //遍历所有卡
    for (var i = 0; i < elements.size(); i++) {
        var element = elements.get(i);
        var fullcardNumSelect = element.select("tr>td:nth-child(3)");
        var fullcardNum="";
        if (fullcardNumSelect.isEmpty()) {
            logger.info("当前卡号为空，停止填充数据");
            continue;
        }
        //每次循环创建，保存每张卡信息
        var cardInfoMap=new HashMap();

        //卡状态
        var cardStatus = element.select("tr > td:nth-child(5)");
        if (!cardStatus.isEmpty()&&cardStatus.html().contains("待添加")) {
            logger.info("当前卡号未添加，停止填充数据");
            continue;
        }
        //卡类别
        var type = element.select("tr>td:nth-child(1)").get(0).text();
        logger.info("type="+type);
        //全卡号
        fullcardNum=fullcardNumSelect.get(0).text();
        //卡状态
        var cardStatus=element.select("td:nth-child(5)").get(0).text();

        //信用卡开户行
        var creditOpenBank=element.select("td:nth-child(4)").get(0).text();
        //借记卡开户行
        var debitOpenBank=element.select("td:nth-child(5)").get(0).text();

        cardInfoMap.put(keys_sh.CARD_TYPE, type);
        cardInfoMap.put(keys_sh.FULL_CARD_NUM, fullcardNum);
        cardInfoMap.put(keys_sh.CARD_STATUS,cardStatus);
        cardInfoMap.put(keys_sh.CRED_OPEN_BANK,creditOpenBank);
        cardInfoMap.put(keys_sh.DEBT_OPEN_BANK, debitOpenBank);

        if("信用卡".equals(type)){
            creditCardInfoList.add(cardInfoMap);
        }
        if ("借记卡".equals(type)){
            debitCardInfoList.add(cardInfoMap);
        }
    }
    //添加卡数量到上下文
    context.put(keys.CRED_CARD_SIZE,creditCardInfoList.size());
    logger.info("size==="+creditCardInfoList.size());
    context.put(keys.DEBT_CARD_SIZE,debitCardInfoList.size());

    //添加信用卡用户信息到上下文
    context.put(keys.CRED_CARD_LIST,creditCardInfoList);
    //添加借记卡用户信息到上下文
    context.put(keys.DEBT_CARD_LIST,debitCardInfoList);
}
}


//上海卡信息键值
var keys_sh={
    //信用卡开户行
    CRED_OPEN_BANK:"cred_open_bank",
    //卡状态
    CARD_STATUS:"card_status",
    //借记卡开户行
    DEBT_OPEN_BANK:"debt_open_bank",
    //卡类别
    CARD_TYPE:"card_type",
    //全卡号
    FULL_CARD_NUM:"full_card_num",
    //存放基础信息的MAP
    BASIC_INFO_MAP:"basic_info_map"

}
